﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MyNet.Library.Shared;
using MyNet.Library.Mvc;
using MyNet.Library.Core;

namespace Coffee.Web
{
    public class OAController : BaseController
    {
        [Power]
        public ActionResult Index(ActionOptions actionOptions)
        {
            actionOptions.ResultType = ActionResultType.View;

            return Result(actionOptions);
        }

        [Power(_needAuthenticated = false)]
        public ActionResult Login(ActionOptions actionOptions)
        {
            if (MyNet.Library.Shared.ManagerAuthorization.Current.IsAuthenticated)
            {
                return Redirect(Url.Action("Index", "OA"));
            }
            return Result(actionOptions);
            //    return Redirect(ManagerAuthorization.Current.LoginUrl);
        }

        [Power(_needAuthenticated = false)]
        public ActionResult LoginForMork(ActionOptions actionOptions, int id)
        {
            actionOptions.ResultType = ActionResultType.Json;

            SafeExcute(delegate
            {
                ManagerAuthorization.Current.Authorize(id, true);
            });

            return Redirect(Url.Action("Index", "OA"));
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="actionOptions"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        [HttpPost]
        [Power(_needAuthenticated = false)]
        public ActionResult Login(ActionOptions actionOptions, string userName, string password, string openId)
        {
            actionOptions.ResultType = ActionResultType.Json;

            SafeExcute(delegate
            {
                var userId = CommonAppService.Current.ValidateUser(new string[] { "WX", "Common", "Admin","MK" }, userName, password);

                var user = CommonAppService.Current.GetUser(userId);

                AssertHelper.NoPass(user == null, "用户名不存在");
                AssertHelper.NoPass(user.IsEnable == false, "帐号已经被停用，请联系管理员");
                ManagerAuthorization.Current.Authorize(user.Id, true);

            });

            return Redirect(Url.Action("Index", "OA"));
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="actionOptions"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="openId"></param>
        /// <returns></returns>

        [Power(_needAuthenticated = false)]
        public ActionResult GetIp(ActionOptions actionOptions)
        {
            actionOptions.ResultType = ActionResultType.Json;
            var ips = new List<string>();
            
            SafeExcute(delegate
            {
                ips.Add(HttpContext.Request.Headers["X-Real-IP"]);
                ips.Add(HttpContext.Request.Headers["HTTP_X_FORWARDED_FOR"]);
                ips.Add(HttpContext.Request.Headers["REMOTE_ADDR"]);
                ips.Add(HttpContext.Request.UserHostAddress);
            });

            return Result(actionOptions, ips);
        }

        [Power(_needAuthenticated = false)]
        public ActionResult GetHeaders(ActionOptions actionOptions)
        {
            actionOptions.ResultType = ActionResultType.Json;
            var ips = new List<string>();

            SafeExcute(delegate
            {
               
            });

            return Result(actionOptions, HttpContext.Request.Headers);
        }

        [Power(_needAuthenticated = false)]
        public ActionResult ClearCache(ActionOptions actionOptions)
        {
            actionOptions.ResultType = ActionResultType.Json;
            var count = CacheHelper.Count();
            SafeExcute(delegate
            {
                CacheHelper.Clear();
            });

            return Result(actionOptions, "清除完成(" + count + "):" + CacheHelper.Count());
        }
    }
}
